﻿using System;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace KEA.Core
{
    class ScrollingBackground
    {
        private Texture2D texture;
        private Vector2 textureSize, screenSize,texturePos;
        private GraphicsDeviceManager graphics;

        public void Load( GraphicsDeviceManager device,Texture2D texture)
        {
            this.texture = texture;
            graphics = device;
            screenSize = new Vector2(Game1.SCREEN_W, Game1.SCREEN_H);
            textureSize = new Vector2(0, texture.Height);
            texturePos = new Vector2(0, screenSize.Y);
        }

        public void Update(float Y)
        {
            texturePos.Y += Y;
            texturePos.Y = (texturePos.Y) % textureSize.Y;
            screenSize = new Vector2(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
            
        }

        public void Draw(SpriteBatch batch)
        {
            Vector2 max = screenSize / textureSize + new Vector2(1,1);
            Vector2 limit = new Vector2((float)Math.Ceiling((float)screenSize.X / (float)texture.Width), (float)Math.Ceiling(-1 + max.Y));
            // Boucle verticale
            for (int i = -1; i < limit.Y; i++)
            {
                // Boucle horizontale
                for (int j = 0; j < limit.X; j++)
                {
                    batch.Draw(texture, texturePos + i * textureSize + new Vector2((j * texture.Width),0), Color.White);
                }
            }
        }

    }
}
